﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevFx.Model;
using System.Linq.Expressions;
using System.Data.Objects.DataClasses;
using System.Data.Objects;
using DevFx.FrameWork.Data;
namespace DevFx.Biz
{
    public class BaseBiz<T> where T : EntityObject
    {
        protected ObjectSet<T> Context
        {
            get
            {
                InitContext();
                return _ctx;
            }
        } 
        private ObjectSet<T> _ctx;

        protected void InitContext()
        {
            Entities db = DbHelper.GetCurrentDb();
           // Entities db = new Entities();
            _ctx = db.CreateObjectSet<T>(typeof(T).Name+"s");
        }
        protected void SubmitChanges()
        {
            try
            {
                _ctx.Context.SaveChanges();
                _ctx.Context.AcceptAllChanges();
            }
            catch
            {
                throw;
            }
            finally
            {
                _ctx.Context.Dispose();
                _ctx = null;
                DbHelper.RemoveCurrentDb();
            }
        }

        public T GetByCondition(Expression<Func<T, bool>> condition)
        {
            return this.Context.Where<T>(condition).FirstOrDefault<T>();
        }

        public List<T> GetList(Expression<Func<T, bool>> condition)
        {
            return this.Context.Where<T>(condition).ToList<T>();
        }

        public List<T> GetList()
        {
            return this.Context.ToList<T>();
        }
        public void Create(T t)
        {
            try
            {
                InitContext();
                _ctx.AddObject(t);
                SubmitChanges();
            }
            catch
            {
                throw;
            }
        }

        public void Update(T t)
        {
            try
            {
                InitContext();
                _ctx.Context.GetObjectByKey(t.EntityKey);
                _ctx.ApplyCurrentValues(t);
                SubmitChanges();
            }
            catch
            {
                throw;
            }
        }

        public void Delete(T t)
        {
            if (t == null) return;
            try
            {
                InitContext();
                _ctx.Context.Attach(t);
                _ctx.Context.DeleteObject(t); 
                SubmitChanges();
            }
            catch
            {
                throw;
            }
        }
        public void Delete(List<T> ts)
        {
            if (ts.Count < 1) return;
            InitContext();

            foreach (var t in ts)
            {
                try
                {
                    _ctx.Context.Attach(t);
                    _ctx.Context.DeleteObject(t);
                }
                catch
                {
                    throw;
                }
            }
            SubmitChanges();
        }
        
    }
}
